import Vue from "vue";
import VueRouter from "vue-router";
import { isLogined } from "../utils/tools";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import("../views/Home.vue"),
  },
  {
    path: "/tour",
    name: "Tour",
    component: () => import("../views/Tour.vue"),
  },
  {
    path: "/user",
    name: "User",
    component: () => import("../views/User.vue"),
    meta: {
      needLogin: true,
    },
  },

  {
    path: "/reg",
    name: "Reg",
    component: () => import("../views/Reg.vue"),
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/new_info",
    name: "NewInfo",
    component: () => import("../views/NewInfo.vue"),
  },
  {
    path: "/change_pwd",
    name: "ChangePwd",
    component: () => import("../views/ChangePwd.vue"),
  },
  {
    path:'/detail',
    name:'Detail',
    component:()=>import('../views/Details.vue')
  }
  ,
  {
    path:'/search',
    name:'Search',
    component:()=>import('../views/Search.vue')
  },{
    path:'/coupons',
    name:'Coupons',
    component:()=>import('../views/Coupons.vue')
  }
];

const router = new VueRouter({
  routes,
});

//全局拦截
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (isLogined()) {
      next();
    } else {
      next({ name: "Login" });
    }
  } else {
    next();
  }
});
export default router;
